package com.gxa.weixing.core.util;

import com.gxa.weixing.core.common.ExceptionCode;
import com.gxa.weixing.core.exception.BusinessException;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 针对map类型传递的参数进行数据转换的工具类
 *
 * @author lijianmin
 * @date 2022/01/06  21:31:13
 */
public class VerifyParamUtil {

    private VerifyParamUtil() {
    }

    /**
     * 页码
     *
     * @return 返回转换后的值或page默认值1
     * @throws BusinessException 操作异常
     */
    public static Integer pageDefaultOneVerify(Object page) throws BusinessException {
        // 设置默认分页条件
        int pages = 1;

        // 如果不为空 则判断数值是否合法和赋值
        if (page != null) {
            try {
                pages = Integer.parseInt(page.toString());
            } catch (Exception e) {
                throw new BusinessException(ExceptionCode.PAGE_OR_SIZE_ERROR);
            }
            if (pages < 1) {
                throw new BusinessException(ExceptionCode.PAGE_OR_SIZE_ZERO);
            }
        }
        return pages;
    }


    /**
     * 页码
     *
     * @return 返回转换后的值或size默认值5
     * @throws BusinessException 操作异常
     */
    public static Integer sizeDefaultFiveVerify(Object size) throws BusinessException {
        // 设置默认分页条件
        int sizes = 5;

        // 如果不为空 则判断数值是否合法和赋值
        if (size != null) {
            try {
                sizes = Integer.parseInt(size.toString());
            } catch (Exception e) {
                throw new BusinessException(ExceptionCode.PAGE_OR_SIZE_ERROR);
            }
            if (sizes < 1) {
                throw new BusinessException(ExceptionCode.PAGE_OR_SIZE_ZERO);
            }
        }
        return sizes;
    }

    /**
     * 页码
     *
     * @return 返回转换后的值或size无限大的值
     * @throws BusinessException 操作异常
     */
    public static Integer sizeInfiniteVerify(Object size) throws BusinessException {
        // 设置默认分页条件
        int sizes = 1000000;

        // 如果不为空 则判断数值是否合法和赋值
        if (size != null) {
            try {
                sizes = Integer.parseInt(size.toString());
            } catch (Exception e) {
                throw new BusinessException(ExceptionCode.PAGE_OR_SIZE_ERROR);
            }
            if (sizes < 1) {
                throw new BusinessException(ExceptionCode.PAGE_OR_SIZE_ZERO);
            }
        }
        return sizes;
    }

    /**
     * 页码
     *
     * @return 返回转换后的值或page默认值null
     * @throws BusinessException 操作异常
     */
    public static Integer pageDefaultVerify(Object page) throws BusinessException {
        // 设置默认分页条件
        Integer pages = null;

        // 如果不为空 则判断数值是否合法和赋值
        if (page != null) {
            try {
                pages = Integer.parseInt(page.toString());
                if (pages < 1) {
                    throw new BusinessException(ExceptionCode.PAGE_OR_SIZE_ZERO);
                }
            } catch (Exception e) {
                throw new BusinessException(ExceptionCode.PAGE_OR_SIZE_ERROR);
            }
        }
        return pages;
    }

    /**
     * 页码
     *
     * @return 返回转换后的值或size默认值null
     * @throws BusinessException 操作异常
     */
    public static Integer sizeDefaultVerify(Object size) throws BusinessException {
        // 设置默认分页条件
        Integer sizes = null;

        // 如果不为空 则判断数值是否合法和赋值
        if (size != null) {
            try {
                sizes = Integer.parseInt(size.toString());
                if (sizes < 1) {
                    throw new BusinessException(ExceptionCode.PAGE_OR_SIZE_ZERO);
                }
            } catch (Exception e) {
                throw new BusinessException(ExceptionCode.PAGE_OR_SIZE_ERROR);
            }
        }
        return sizes;
    }

    /**
     * 页码
     *
     * @return 返回当前值或page默认值null
     * @throws BusinessException 操作异常
     */
    public static Integer pageVerify(Integer page) throws BusinessException {
        // 设置默认分页条件
        Integer pages = null;

        // 如果不为空 则判断数值是否合法和赋值
        if (page != null) {
            if (page < 1) {
                throw new BusinessException(ExceptionCode.PAGE_OR_SIZE_ZERO);
            }
            pages = page;
        }
        return pages;
    }

    /**
     * 页码
     *
     * @return 返回当前值或size默认值null
     * @throws BusinessException 操作异常
     */
    public static Integer sizeVerify(Integer size) throws BusinessException {
        // 设置默认分页条件
        Integer sizes = null;

        // 如果不为空 则判断数值是否合法和赋值
        if (size != null) {
            if (size < 1) {
                throw new BusinessException(ExceptionCode.PAGE_OR_SIZE_ZERO);
            }
        }
        return sizes;
    }

    /**
     * 页码
     *
     * @return 返回当前值或page默认值1
     * @throws BusinessException 操作异常
     */
    public static Integer pageOneVerify(Integer page) throws BusinessException {
        // 设置默认分页条件
        int pages = 1;

        // 如果不为空 则判断数值是否合法和赋值
        if (page != null) {
            if (page < 1) {
                throw new BusinessException(ExceptionCode.PAGE_OR_SIZE_ZERO);
            }
            if (page == 1) {
                return page;
            }
            pages = page;
        }
        return pages;
    }

    /**
     * 页码
     *
     * @return 返回当前值或size默认值null
     * @throws BusinessException 操作异常
     */
    public static Integer sizeFiveVerify(Integer size) throws BusinessException {
        // 设置默认分页条件
        int sizes = 5;

        // 如果不为空 则判断数值是否合法和赋值
        if (size != null) {
            if (size < 1) {
                throw new BusinessException(ExceptionCode.PAGE_OR_SIZE_ZERO);
            }
            if (size == 1) {
                return size;
            }
        }
        return sizes;
    }

    /**
     * String类型参数
     *
     * @return 返回转换后的值或默认值
     */
    public static String stringVerifyParam(Object stringParameter) throws BusinessException {
        String param = null;
        if (stringParameter != null) {
            try {
                param = stringParameter.toString();
            } catch (NumberFormatException e) {
                throw new BusinessException(ExceptionCode.FORMAT_COMVERSION_ERROR);
            }
        }
        return param;
    }

    /**
     * Integer类型参数
     *
     * @return 返回转换后的值或默认值
     */
    public static Integer integerVerifyParam(Object integerParameter) throws BusinessException {
        Integer param = null;
        if (integerParameter != null) {
            try {
                param = Integer.parseInt(integerParameter.toString());
            } catch (NumberFormatException e) {
                throw new BusinessException(ExceptionCode.FORMAT_COMVERSION_ERROR);
            }
        }
        return param;
    }

    /**
     * Boolean类型参数
     *
     * @return 返回转换后的值或默认值
     */
    public static Boolean booleanVerifyParam(Object booleanParameter) throws BusinessException {
        Boolean param = null;
        if (booleanParameter != null) {
            try {
                param = Boolean.parseBoolean(booleanParameter.toString());
            } catch (NumberFormatException e) {
                throw new BusinessException(ExceptionCode.FORMAT_COMVERSION_ERROR);
            }
        }
        return param;
    }

    /**
     * Long类型参数
     *
     * @return 返回转换后的值或默认值
     */
    public static Long longVerifyParam(Object longParameter) throws BusinessException {
        Long param = null;
        if (longParameter != null) {
            try {
                param = Long.valueOf(longParameter.toString());
            } catch (NumberFormatException e) {
                throw new BusinessException(ExceptionCode.FORMAT_COMVERSION_ERROR);
            }
        }
        return param;
    }

    /**
     * Date类型参数
     *
     * @return 返回转换后的值或默认值
     */
    public static Date dataVerifyParam(Object dateParameter) throws BusinessException {
        Date param = null;

        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        if (dateParameter != null) {
            try {
                param = df.parse(dateParameter.toString());
            } catch (NumberFormatException | ParseException e) {
                throw new BusinessException(ExceptionCode.FORMAT_COMVERSION_ERROR);
            }
        }
        return param;
    }

    /**
     * BigDecimal类型参数
     *
     * @return 返回转换后的值或默认值
     */
    public static BigDecimal bigDecimalVerifyParam(Object bigDecimalParameter) throws BusinessException {
        BigDecimal param = null;
        if (bigDecimalParameter != null) {
            try {
                param = new BigDecimal(bigDecimalParameter.toString());
            } catch (NumberFormatException e) {
                throw new BusinessException(ExceptionCode.FORMAT_COMVERSION_ERROR);
            }
        }
        return param;
    }

    /**
     * Byte类型参数
     *
     * @return 返回转换后的值或默认值
     */
    public static Byte byteVerifyParam(Object byteParameter) throws BusinessException {
        Byte param = null;
        if (byteParameter != null) {
            try {
                param = Byte.parseByte(byteParameter.toString());
            } catch (NumberFormatException e) {
                throw new BusinessException(ExceptionCode.FORMAT_COMVERSION_ERROR);
            }
        }
        return param;
    }

    /**
     * Short类型参数
     *
     * @return 返回转换后的值或默认值
     */
    public static Short shortVerifyParam(Object shortParameter) throws BusinessException {
        Short param = null;
        if (shortParameter != null) {
            try {
                param = Short.parseShort(shortParameter.toString());
            } catch (NumberFormatException e) {
                throw new BusinessException(ExceptionCode.FORMAT_COMVERSION_ERROR);
            }
        }
        return param;
    }

    /**
     * Double类型参数
     *
     * @return 返回转换后的值或默认值
     */
    public static Double doubleVerifyParam(Object doubleParameter) throws BusinessException {
        Double param = null;
        if (doubleParameter != null) {
            try {
                param = Double.parseDouble(doubleParameter.toString());
            } catch (NumberFormatException e) {
                throw new BusinessException(ExceptionCode.FORMAT_COMVERSION_ERROR);
            }
        }
        return param;
    }

    /**
     * Float类型参数
     *
     * @return 返回转换后的值或默认值
     */
    public static Float floatVerifyParam(Object floatParameter) throws BusinessException {
        Float param = null;
        if (floatParameter != null) {
            try {
                param = Float.parseFloat(floatParameter.toString());
            } catch (NumberFormatException e) {
                throw new BusinessException(ExceptionCode.FORMAT_COMVERSION_ERROR);
            }
        }
        return param;
    }

    /**
     * Character类型参数
     *
     * @return 返回转换后的值或默认值
     */
    public static Character characterVerifyParam(Object characterParameter) throws BusinessException {
        Character param = null;
        if (characterParameter != null) {
            try {
                param = (Character) characterParameter;
            } catch (NumberFormatException e) {
                throw new BusinessException(ExceptionCode.FORMAT_COMVERSION_ERROR);
            }
        }
        return param;
    }

    /**
     * Timestamp类型参数
     *
     * @return 返回转换后的值或默认值
     */
    public static Timestamp timestampVerifyParam(Object timestampParameter) throws BusinessException {
        Timestamp param = null;
        if (timestampParameter != null) {
            try {
                param = Timestamp.valueOf(timestampParameter.toString());
            } catch (NumberFormatException e) {
                throw new BusinessException(ExceptionCode.FORMAT_COMVERSION_ERROR);
            }
        }
        return param;
    }

    /**
     * 通用转换工具 好像有异常
     *
     * @param param 值
     * @return 返回转换后的值
     * @throws Exception 操作中产生的异常
     */
    public static <T> T verifyParam(Object param) throws Exception {
        // 根据泛型获取泛型的实例对象
        T t = null;
        // 如果不为空则进行强转
        if (param != null) {
            try {
                // 使用try catch 对强转可能产生的异常进行捕获
                t = (T) param;
            } catch (NumberFormatException e) {
                throw new BusinessException(ExceptionCode.FORMAT_COMVERSION_ERROR);
            }
        }
        // 返回该泛型对象
        return t;
    }


    /**
     * 校验字段是否为空
     *
     * @param params 参数 可以为多个
     * @throws BusinessException 参数为空的提示信息
     */
    public static void checkParams(Object... params) throws BusinessException {
        for (Object param : params) {
            if (param == null) {
                throw new BusinessException(ExceptionCode.PARAM_IS_NULL);
            }
        }
    }


    /**
     * 转换时间格式为标准格式
     *
     * @param param 参数
     * @return 返回
     * @throws BusinessException 操作中产生的异常
     */
    public static String formatDate(Object param) throws BusinessException, ParseException {
        // 判断参数是否为空
        if (param == null) {
            throw new BusinessException(ExceptionCode.FORMAT_COMVERSION_ERROR);
        }
        // 创建日期转换格式的对象
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        // 如果不为空 转换为string类型
        // 返回字符串
        return df.format(df.parse(param.toString()));
    }


    /**
     * 转换时间格式为标准格式
     *
     * @param params 参数
     * @return 返回
     * @throws BusinessException 操作中产生的异常
     */
    public static List<Map<String, Object>> formatDateToList(List<Map<String, Object>> list, String... params) throws BusinessException {
        // 创建日期转换格式的对象
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 遍历list
        for (Map<String, Object> map : list) {
            // 遍历传递的参数是否为空
            for (String param : params) {
                checkParams(map.get(param));
                // 循环修改时间字段
                map.put(param,df.format(map.get(param)));
            }
        }
        // 返回该list
        return list;

    }

}
